/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package mbean.admin.vehicle;

import javax.inject.Named;
import javax.enterprise.context.SessionScoped;
import java.io.Serializable;
import java.util.List;
import javax.ejb.EJB;
import model.entity.Depot;
import model.entity.Vehicle;
import org.omnifaces.util.Faces;
import org.omnifaces.util.Messages;
import service.InterfaceServiceLocal;

/**
 *
 * @author dingnangen
 */
@Named(value = "vehicleCreateMB")
@SessionScoped
public class VehicleCreateMB implements Serializable {

    @EJB(beanName = "VehicleManager")
    private InterfaceServiceLocal vehicleManager;
    @EJB(beanName = "DepotManager")
    private InterfaceServiceLocal depotManager;
    private Vehicle currentVehicle;
    private List<Depot> depotList;

    public void init() {
        depotList = depotManager.findAll();
        currentVehicle = new Vehicle();
        System.out.println("currentVehicle:"+currentVehicle.getIddepot());
    }

    public void precreateVehicle() {
        if (Faces.isPostback()) {
            System.out.println("isPostback");
            return;
        }

        init();
    }

    public void create() {
        vehicleManager.create(this.currentVehicle);
        Messages.create(Faces.getResourceBundle("bundle").getString("saveSuccess")).add();
        init();
    }

    /**
     * Creates a new instance of VehicleCreateMB
     */
    public VehicleCreateMB() {
    }

    public Vehicle getCurrentVehicle() {
        return currentVehicle;
    }

    public void setCurrentVehicle(Vehicle currentVehicle) {
        this.currentVehicle = currentVehicle;
    }

    public List<Depot> getDepotList() {
        return depotList;
    }

    public void setDepotList(List<Depot> depotList) {
        this.depotList = depotList;
    }
}
